﻿using System.Linq.PageList;
using Dyao.Book.Application.Abstractions;
using Dyao.Book.Contract;
using Dyao.Book.Core;
using Dyao.Book.Web.Extensions;
using Microsoft.AspNetCore.Mvc;

namespace Dyao.Book.Web.Controllers
{
    public class BookMgrController : ApiV1BaseController
    {
        public readonly IBookMgrApplication _bookMgrApplication;
        public BookMgrController(IBookMgrApplication bookMgrApplication)
        {
            _bookMgrApplication = bookMgrApplication;
        }

        [HttpPost("[action]")]
        public async Task<ApiResult<PageList<BookResultDto>>> GetPageListAsync(BookFilterDto filterModel, CancellationToken cancellationToken = default)
        {
            var filter = filterModel;
            var pageFilter = filter.ToPageFilter(filter);
            return (PageList<BookResultDto>)await _bookMgrApplication.GetPageListAsync(pageFilter, cancellationToken);
        }

        /// <summary>
        /// 添加书籍信息
        /// </summary>
        /// <param name="book">书籍信息</param>
        /// <param name="cancellationToken">取消令牌</param>
        /// <returns></returns>
        [HttpPost("[action]")]
        public async Task<ApiResult<string>> AddAsync(BookInfoDto book, CancellationToken cancellationToken = default)
        {
            this.ValidateModelState();
            var msg = await _bookMgrApplication.AddAsync(book, cancellationToken);
            return msg;
        }

        /// <summary>
        /// 修改书籍信息
        /// </summary>
        /// <param name="book">书籍信息</param>
        /// <param name="cancellationToken">取消令牌</param>
        /// <returns></returns>
        [HttpPost("[action]")]
        public async Task<ApiResult<string>> ModifyAsync(BookInfoDto book, CancellationToken cancellationToken = default)
        {
            this.ValidateModelState();
            var msg = await _bookMgrApplication.ModifyAsync(book, cancellationToken);
            return msg;
        }

        /// <summary>
        /// 删除书籍信息
        /// </summary>
        /// <param name="bookId">书籍编号</param>
        /// <param name="cancellationToken">取消令牌</param>
        /// <returns></returns>
        [HttpPost("[action]")]
        public async Task<ApiResult<string>> DeleteAsync(CommonIdDto bookId, CancellationToken cancellationToken = default)
        {
            this.ValidateModelState();
            var msg = await _bookMgrApplication.DeleteAsync(bookId.Id, cancellationToken);
            return msg;
        }
    }
}
